// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//

#include <cantor/parser/parser.h>
#include <cantor/transform/dqls/table_source.h>
#include <cantor/transform/dqls/index_hint.h>
#include <cantor/transform/clause/table_name.h>
#include <cantor/transform/expr.h>
#include <cantor/transform/stmt.h>

namespace cantor::transform {
    void TransTableSource::resolve_ast(const cantor::parser::TableSource *node) {
        if (!node) {
            status = turbo::data_loss_error("no expr");
            return;
        }
        /// table_name
        if (node->table_name) {
            status = TransTableName::resolve_to(node->table_name, table_source.mutable_table_name());
        }
        if (!status.ok()) {
            return;
        }
        /// as_name
        table_source.set_as_name(node->as_name.to_string());
        /// derived_table
        if (node->derived_table) {
            status = TransStmt::resolve_to(node->derived_table, table_source.mutable_derived_table());
        }
        if (!status.ok()) {
            return;
        }
        // index_hints
        auto hints = table_source.mutable_index_hints();
        hints->Reserve(node->index_hints.size());
        for (size_t i = 0; i < node->index_hints.size(); ++i) {
            TransIndexHint trans;
            trans.resolve_ast(node->index_hints[i]);
            status = trans.check();
            if (!status.ok()) {
                return;
            }
        }

        /// partition_names
        auto parts = table_source.mutable_partition_names();
        parts->Reserve(node->partition_names.size());
        for (size_t i = 0; i < node->partition_names.size(); ++i) {
            parts->Add()->assign(node->partition_names[i].to_string());
        }

        set_proto_location(node, table_source.mutable_location());
        set_proto_status(table_source.mutable_status());
    }

    const turbo::Status &TransTableSource::check() {
        if (!status.ok()) {
            return status;
        }
        if (!table_source.has_location()) {
            make_missing_required_field("location");
            return status;
        }
        return status;
    }

    turbo::Status TransTableSource::resolve_to(turbo::Nonnull<const cantor::parser::TableSource *> node, turbo::Nonnull<pb::ast::TableSource *> out) {
        TransTableSource trans;
        trans.resolve_ast(node);
        auto status = trans.check();
        if (!status.ok()) {
            return status;
        }
        *out = std::move(trans.table_source);
        return status;
    }

    turbo::Status TransTableSource::resolve_to(turbo::Nonnull<const cantor::parser::TableSource *> node, turbo::Nonnull<pb::ast::TableRef *> out) {
        return resolve_to(node, out->mutable_table_source());
    }
} // namespace cantor::transform
